﻿var rtdataHost = "/web/graph.php";//http://192.168.1.76
$(document).ready(function() {
    $("#type").bind("change", function() {
        if ($(this).val() == 0) {
            $("#labelend").hide();
            $('#end').val('');
//            $("#rp1,#rp2,#rp3,#rp4").show();
//            $("#rp6,#rp7,#rp5").hide();
            $("#labeldaytoolbar").show();
            $("#labelmonthtoolbar,#labeltimetoolbar").hide();
            $("#condition1").trigger("click");
        }
        else if ($(this).val() == 1) {
            $("#labelend").hide();
            $('#end').val('');
//            $("#rp1,#rp2,#rp3,#rp4,#rp5,#rp6,#rp7").show();
//            $("#rp5").hide();
            $("#labelmonthtoolbar").show();
            $("#labeldaytoolbar,#labeltimetoolbar").hide();
            $("#condition4").trigger("click");
        }
        else {
            $("#labelend").show();
//            $("#rp1,#rp3").show();
//            $("#rp2,#rp4,#rp7,#rp5,#rp6").hide();
            $("#labeltimetoolbar").show();
            $("#labelmonthtoolbar,#labeldaytoolbar").hide();
            $("#condition6").trigger("click");
        }
    });
    $("#type").trigger("change");
});
function validate(start, end) {
    var type = $('#type').val();
    if (type == "0") {
        if (!checkDate(start, 8)) {
            alert("请填写正确的日期！");
            return false;
        }
    } else if (type == "1") {
        if (!checkDate(start, 6)) {
            alert("请填写正确的日期！");
            return false;
        }
    } else {
        if (!checkDate(start, 8)) {
            alert("请填写正确的日期！");
            return false;
        }
        if (!checkDate(end, 8)) {
            alert("请填写正确的日期！");
            return false;
        }
        if (start > end) {
            alert("请填写正确的开始、结束日期！");
            return false;
        }
    }
    return true;
}
function showall(){
    var html = "<table>"
	+"<tr>"
	    +"<td>"//handled_request, active_user, hit, user_request, bandwidth, back_source
	     +view('cpu')+"<br>"
	     +view('mem')+"<br>"
	     +view('disk')+"<br>"
	     +view('net')+"<br>"
	     //+view('back_source')
	    +"</td>"
	+"</tr>"
	+"</table>";
	 $("#chartsdiv").html(html);
}
function view(dsname) {
    var watermark = "www.soooner.com";
    var width = 700;
    var height = 200;
    var stitle = 11;
    var title = "";
    var type = $('#type').val();
    var rrdfile = "report";//getRrdfile(sp,owner);   
    var start = $('#start').val();
    var end = $('#end').val();
    var color = getColor(dsname);
    var xlable = getTitle(dsname);
    if (validate(start, end)) {
         if(type =="0")//日报/实时监测
         {
            end = start;
         }
         else if(type == "1")//月报
         {
            var y="2000";var m = "01";
            y = start.substring(0,4);
            m = start.substring(4,6);
            var days = getDays(y,m);
            start = start+"01";
            end = y+m+days;
         }
         else//时间段
         {}
         if(type=="0") title = xlable+"("+start+")";
         else title = xlable+"("+start+" - "+end+")";
         var t = Math.random();
         if(type == "0")
         {
            var date = new Date();
            if(start == date.toString("yyyyMMdd"))
            { t=Math.random(); }
         }
        var xgrid = getXgrid(type,start,end);
        var src = "";
        var units_exponent="";
        if(dsname!="net")
            units_exponent = "&units-exponent=0";
        else
            units_exponent = "&base=1024";
        src = rtdataHost+"?t="+t+"&stitle="+stitle+"&watermark="+watermark+"&width="+width+"&height="+height+"&title="+title+"&ds="+dsname+"|"+rrdfile+"|AREA|"+color+"|"+xlable+"&start="+start+" 00:00&end="+end+" 23:55&xgrid="+xgrid+units_exponent;
//        $("#chartsdiv").html("<img src='"+src+"'>");
        return "<img src='"+src+"'>";
    }
    else {
//        $("#chartsdiv").html("");
//        $("#datadiv").html("");
        return "error!";
    }
}
function show(dsname) {
    var watermark = "www.soooner.com";
    var width = 700;
    var height = 200;
    var stitle = 11;
    var title = "";
    var type = $('#type').val();
    var rrdfile = "report";//getRrdfile(sp,owner);   
    var start = $('#start').val();
    var end = $('#end').val();
    var color = getColor(dsname);
    var xlable = getTitle(dsname);
    if (validate(start, end)) {
         if(type =="0")//日报/实时监测
         {
            end = start;
         }
         else if(type == "1")//月报
         {
            var y="2000";var m = "01";
            y = start.substring(0,4);
            m = start.substring(4,6);
            var days = getDays(y,m);
            start = start+"01";
            end = y+m+days;
         }
         else//时间段
         {}
         if(type=="0") title = xlable+"("+start+")";
         else title = xlable+"("+start+" - "+end+")";
         var t = Math.random();
         if(type == "0")
         {
            var date = new Date();
            if(start == date.toString("yyyyMMdd"))
            { t=Math.random(); }
         }
        var xgrid = getXgrid(type,start,end);
        var units_exponent="";
        if(dsname!="net")
            units_exponent = "&&units-exponent=0";
        else
            units_exponent = "&base=1024";
        var src = rtdataHost+"?t="+t+"&stitle="+stitle+"&watermark="+watermark+"&width="+width+"&height="+height+"&title="+title+"&ds="+dsname+"|"+rrdfile+"|AREA|"+color+"|"+xlable+"&start="+start+" 00:00&end="+end+" 23:55&xgrid="+xgrid+units_exponent;
        $("#chartsdiv").html("<img src='"+src+"'>");
    }
    else {
        $("#chartsdiv").html("");
        $("#datadiv").html("");
    }  
    return false;
}
function getXgrid(type,start,end)
{
    //xgrid=HOUR:6:DAY:1:HOUR:12:0:%d日%H时
    var xgrid = "";
    if(type=="1")//月报
    {
        xgrid="HOUR:12:DAY:1:DAY:1:0:%d";
    }
    else if(type=="2")//时间段
    {
        var days = getnumber(start,end);
        if(days>1&&days<=3)//<3天 12小时/标签
        {
            xgrid="HOUR:6:DAY:1:HOUR:12:0:%d(%H:00)";
        }
        else if(days>3&&days<=31)//按1天/标签
        {
            xgrid="HOUR:12:DAY:1:DAY:1:0:%d";
        }
        else if(days>31&&days<=62) //按 7天/标签
        {
            xgrid="DAY:1:DAY:1:DAY:7:0:%m-%d";
        }
        else if(days>62&&days<=365) //按 1月/标签
        {
            xgrid="DAY:1:MONTH:1:MONTH:1:0:%m";//%b
        }
        else{}
    }
    else{}
    return xgrid;
}
function getDays(y,m)
{
    return new Date(y,m,0).getDate();
}
function getTitle(dsname)
{
    var title ="real time monitor";
    switch(dsname) //handled_request, active_user, hit, user_request, bandwidth, back_source
    { 
        case "cpu": 
            title ="CPU使用率%";
        break;
        case "mem": 
            title ="内存使用率%";
        break;
        case "disk": 
            title ="空闲硬盘%";
        break;
        case "net": 
            title ="网卡流量";
        break;
        case "live": 
            title ="直播在线人数";
        break;
        case "vod":
            title ="点播在线人数";
        break;
    }
    return title;
}
function getColor(dsname)
{
    var color ="00CC33";
    switch(dsname) //handled_request, active_user, hit, user_request, bandwidth, back_source
    { 
        case "cpu": 
            color ="778899";
        break;
        case "mem": 
            color ="32CD32";
        break;
        case "disk": 
            color ="FF0000";
        break;
        case "net": 
            color ="1E90FF";
        break;
        case "live": 
            color ="411445";
        break;
        case "vod":
            color ="FF8C00";
        break;
    }
    return color;
}
function getDate(addDay) {
    var d = new Date();
    d.setTime(d.getTime() + addDay * 1000 * 60 * 60 * 24);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    if (month < 10) month = "" + 0 + month;
    if (day < 10) day = "" + 0 + day;
    return "" + year + month + day;
}
function getMonth(addMonth) {
    var d = new Date();
    var day = d.getDate();
    if (day == 31)
        d.setDate(day - 2);
    d.setMonth(d.getMonth() + addMonth);
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    if (month < 10) month = "" + 0 + month;
    return "" + year + month;
}
function setDate(start, end) {
    $('#start').val(start);
    $('#end').val(end);
    return false;
}
function checkDate(strDate, strLen) {
    //type=0 8位日期，1 6位月份，2 8位日期
    var check = false;
    if (strLen == 8) {
        var re = new RegExp("^\\d{8}$");
        if (re.test(strDate)) {
            var yyyy = parseInt(strDate.substr(0, 4), 10);
            var mm = parseInt(strDate.substr(4, 2), 10);
            var dd = parseInt(strDate.substr(6, 2), 10);
            var xdata = new Date(yyyy, mm - 1, dd);
            if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
                check = true;
            else
                check = false;
        } else
            check = false;
    }
    else {
        var re = new RegExp("^\\d{6}$");
        if (re.test(strDate)) {
            var yyyy = parseInt(strDate.substr(0, 4), 10);
            var mm = parseInt(strDate.substr(4, 2), 10);
            var dd = 1;
            var xdata = new Date(yyyy, mm - 1, dd);
            if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
                check = true;
            else
                check = false;
        } else
            check = false;
    }
    return check;
}

function getnumber(date1,date2)
{
    //默认格式为"20030303",根据自己需要改格式和方法
    var year1 =  date1.substr(0,4);
    var year2 =  date2.substr(0,4); 
    var month1 = date1.substr(4,2);
    var month2 = date2.substr(4,2);

    var day1 = date1.substr(6,2);
    var day2 = date2.substr(6,2);

    temp1 = year1+"/"+month1+"/"+day1;
    temp2 = year2+"/"+month2+"/"+day2;

    var dateaa= new Date(temp1); 
    var datebb = new Date(temp2); 
    var date = datebb.getTime() - dateaa.getTime(); 
    var time = Math.floor(date / (1000 * 60 * 60 * 24));
    //alert(time);
    return time;
}